﻿using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZP.Common;
using ZP.DTO.Common;
using ZP.IBLL;
using ZP.Model;

namespace ZP.BLL
{
    public class ep_userSerevice : BaseService<ep_user, int>, Iep_userSerevice
    {
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public PortalUserLoginDTO Login(AccountLoginDTO model)
        {
            PortalUserLoginDTO result = new PortalUserLoginDTO();
            //获得用户
            var md5Passwrod = Utils.MD5Encrypt(model.Password);
            var epUser = _repository.GetModels(p => p.state == ZPFlagType.YES && p.account == model.Account && p.password == md5Passwrod).FirstOrDefault();
            //用户名或密码错误
            if (epUser == null)
            {
                return null;
            }
            ////账号被锁定
            //if (sysUser.isLocked.HasValue && sysUser.isLocked.Value == (int)ZPUserLockedType.Locked)
            //{
            //    result.isLocked = sysUser.isLocked.Value;
            //    return result;
            //}
            ////账号被停用
            //if (sysUser.state == ZPFlagType.NO)
            //{
            //    result.state = sysUser.state;
            //    return result;
            //}

            //初始化企业数据
            if (!_ep_user_relDAL.GetModels(p => p.user_id == epUser.user_id).Any())
            {
                _ep_user_relDAL.Add(new ep_user_rel { user_id = epUser.user_id, ep_id = epUser.user_id, is_delete = ZPFlagType.NO }, true);
            }
            if (!_ep_base_infoDAL.GetModels(p => p.ep_id == epUser.user_id).Any())
            {
                _ep_base_infoDAL.Add(new ep_base_info { ep_id = epUser.user_id }, true);
            }
            result = Mapper.Map<PortalUserLoginDTO>(epUser);
            return result;
        }
    }
}
